/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.titan.component.ActionViewException;
import edu.ccut.titan.component.DefaultComponent;
import edu.ccut.titan.component.IActionComponentInfo;
import edu.ccut.titan.component.IActionContext;
import edu.ccut.titan.component.INodeInfo;
import edu.ccut.titan.component.IParameterContext;
import edu.ccut.titan.component.TitanComponentException;

/**
 * Action component use to implement component Type with action.
 * 
 * <p>
 * Action component like a action for handle a use request from web application.
 * It can invoke logic component for handle real business logic or invoke a
 * sub-action component. Finally, it return the next page path for servlet
 * dispatch by throw a {@link ActionViewException}
 * 
 * @author LeslieGu
 * 
 */
public class TitanActionComponent extends DefaultComponent {

	private IActionComponentInfo actionComponentInfo;

	public TitanActionComponent(IActionComponentInfo actionComponentInfo) {
		super(actionComponentInfo.getFullKey());
		this.actionComponentInfo = actionComponentInfo;
	}

	/**
	 * 
	 * @exception ActionViewException
	 */
	public void execute(IParameterContext parameterContext) throws Exception {
		IActionContext actionContext = this
				.createActionContext(parameterContext);

		doExecute(actionContext);
	}

	private IActionContext createActionContext(
			IParameterContext parameterContext) {
		HttpServletRequest request = (HttpServletRequest) parameterContext
				.getParameterValue(0);
		HttpServletResponse response = (HttpServletResponse) parameterContext
				.getParameterValue(1);

		TitanActionContext actionContext = new TitanActionContext(
				this.actionComponentInfo.getFullKey(), request, response);
		actionContext.setValue("request", request);
		actionContext.setValue("response", response);

		logger.debug("create actoin context in process \"" + super.getComponentFullKey() + "\"");
		return actionContext;
	}

	private void doExecute(IActionContext actionContext) throws Exception {
		INodeInfo startNodeInfo = this.actionComponentInfo.getStartNodeInfo();

		String nextId = null;
		INodeInfo nodeInfo = startNodeInfo;
		
		do {
			if (nodeInfo == null) {
				throw new TitanComponentException("can't find node id = "
						+ nextId + "in action component "
						+ this.actionComponentInfo.getFullKey());
			}

			nodeInfo.execute(actionContext);
			
			nextId = nodeInfo.getNextId();
			nodeInfo = this.actionComponentInfo.getNodeInfo(nextId);
		} while (nextId != null);
	}
}
